package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.Normalizer;

public class BddView {
	private String url ;
	private String login ;
	private String password ;
	private Connection connection;
	
	public BddView(){
		this.url = "jdbc:mysql://localhost/GogoleBoard";
		this.login = "root";
		this.password = "root";
		}

	/**
	 * Effectue la requete permettant de se connecter a la bdd
	 */
	public void connectBdd() {
		try{
			this.connection = DriverManager.getConnection(url,login,password);
		}catch(SQLException sqle){
			System.out.println("Exception lors de la connection a la BDD "+sqle.getMessage());
			System.exit(1);
		}
	}

	/**
	 * Effectue la requete permettant d'afficher le contenu de la bdd 
	 * @return le resultat de la requete
	 */
	public ResultSet printBdd() {
		java.sql.Statement stmt;
		ResultSet rs = null;
		try {
			stmt = this.connection.createStatement();
			String sql = "SELECT * FROM Mots";
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}catch (NullPointerException e){
			System.out.println("Is the connection to the DB made ? "+e.toString());
			System.exit(1);
		}
		return rs;

		
	}

	public ResultSet existeInBDD(String mot, String document) {
		java.sql.Statement stmt;
		ResultSet rs = null;
		try {
			stmt = this.connection.createStatement();
			String sql = "SELECT * FROM Mots WHERE Mot = '"+mot+"' AND Document = '"+document+"'";
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}catch (NullPointerException e){
			System.out.println("error ExisteInBdd "+e.toString());
			System.exit(1);
		}
		return rs;
	}

	public ResultSet getPonderation(String mot, String document) {
		java.sql.Statement stmt;
		ResultSet rs = null;
		
		mot = mot.replace("'"," ");

		try {
			stmt = this.connection.createStatement();
			String sql = "SELECT * FROM Mots WHERE Mot = '"+mot+"' AND Document = '"+document+"'";
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace(); 
		}catch (NullPointerException e){
			System.out.println("error GetPonderation "+e.toString());
			System.exit(1);
		}
		return rs;
	}

	public void setPonderation(String mot, String document, int ponderation) {
		java.sql.Statement stmt;
		int rs;
		try {
			stmt = this.connection.createStatement();
			String sql = "UPDATE Mots " +
					"SET Ponderation =  +'"+ponderation+
					"' WHERE Mot = '"+mot +"' AND Document ='"+document+"'";
			rs = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace(); 
		}catch (NullPointerException e){
			System.out.println("error SetPonderation "+e.toString());
			System.exit(1);
		}		
	}

	public void ajouterMot(String mot, String document, int ponderation) throws SQLException {
		java.sql.Statement stmt;
		int rs;
		
		try {
			stmt = this.connection.createStatement();
			String sql = "INSERT INTO Mots VALUES ('"+mot+"','"+document+"','"+ponderation+"')";
			//System.out.println(sql);
			rs = stmt.executeUpdate(sql);
		}catch (NullPointerException e){
			System.out.println("error ajouterMot "+e.toString());
			System.exit(1);
		}
	}
	
	public void deleteMot(String mot){
		java.sql.Statement stmt;
		int rs;
		
		try {
			stmt = this.connection.createStatement();
			String sql = "DELETE FROM Mots WHERE Mots.mot = '"+mot+"'";
			rs = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace(); 
		}catch (NullPointerException e){
			System.out.println("error deleteMot "+e.toString());
			System.exit(1);
		}
	}
}
